/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utfpr.cp.model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

/**
 *
 * @author Adriano_2
 */
public class URLTargetDAO extends DAO {

    public URLTargetDAO(EntityManager em) {
        super(em);
    }
    
    public List<URLTarget> getAll() {
        Query q = em.createQuery("SELECT r FROM URLTarget r ORDER BY r.url");
        try {
            return q.getResultList();
        } catch (NoResultException e) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
        }
        return new ArrayList<>();
    }
    
    public URLTarget getNextURL() {
        return getNextURL(false);
    }
    
    public URLTarget getNextURL(boolean started) {
        Query q = em.createQuery("SELECT r FROM URLTarget r WHERE "
                + " r.finished = false AND r.hasError = false AND r.started = :started"
                + " ORDER BY r.started DESC");
        q.setParameter("started", started);
        q.setMaxResults(1);
        return (URLTarget) q.getSingleResult();
    }
    
    public URLTarget getByURL(String url) {
        Query q = em.createQuery("SELECT e FROM URLTarget e WHERE url = :url");
        q.setParameter("url", url);
        try {
            return (URLTarget) q.getSingleResult();
        } catch (NoResultException e) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
            return new URLTarget();
        }
    }
    
    public boolean save(String url) {
        if (url.isEmpty()) return false;
        
        String newurl;
        if (url.toLowerCase().startsWith("http")) {
            newurl = url.substring(7).trim();
        }
        else {
            newurl = url.toLowerCase().trim();
        }
        
        if (newurl.contains("/")) {
            newurl = newurl.substring(0, newurl.indexOf("/"));
        }
        
        if (newurl.contains(" ")) {
            return false;
        }
        
        if (!newurl.startsWith("www") && getByURL("http://www." + newurl).exist()) {
            return true;
        }
        else if (newurl.startsWith("www") && getByURL("http://" + newurl.substring(4)).exist()){
            DAO.delete(getByURL("http://" + newurl.substring(4)), em);
        }
        newurl = "http://" + newurl;
        
        if (!getByURL(newurl).exist()) {
            return DAO.save(new URLTarget(newurl), em);
        }
        
        return true;
    }
    
    public long getTotalSite() {
        try {
            Query q = em.createQuery("SELECT count(u) FROM URLTarget u");
            return ((Long) q.getSingleResult() ).longValue();
        }
        catch (Exception e) {
            return -1;
        }
    }
    
    public static URLTargetDAO factory() {
        return new URLTargetDAO(getEntity());
    }
}
